package com.itheima.health.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.itheima.health.mapper.OrderMapper;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisMessageConstant;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper,Order> implements OrderService {

    @Reference
    private OrderSettingService orderSettingService;

    @Reference
    private MemberService memberService;

    @Override
    @LcnTransaction
    public Order add(Map map) {
        String codeFromMap = (String) map.get("validateCode");
        String telephone = (String) map.get("telephone");
        String redisCode = RedisUtil.get(RedisMessageConstant.SENDTYPE_ORDER + telephone);
        //1.校验验证码是否正确
        if(StringUtils.isBlank(redisCode)){
            //  过期
            throw  new RuntimeException("验证码过期");
        }
        if(StringUtils.isBlank(codeFromMap)){
            //  表单验证码null
            throw  new RuntimeException("验证码必须填写");
        }
        if(!redisCode.equals(codeFromMap)){
            //  验证码错误
            throw  new RuntimeException("验证码错误");
        }


        //2.根据日期判断预约是否已满
        String orderdate = (String) map.get("orderDate");
        int count = orderSettingService.isOrderOk(orderdate);
        if (count <= 0) {
            throw new RuntimeException("当天预约已满!");
        }

        //3.判断是否为老用户
        Member member = memberService.findMemberByTelephone(telephone);
        String sid = (String) map.get("setmealId");
        int mid;
        if (member != null) {
            //老用户,进一步判断是否重复预约
            //通过订单id,时间,会员id来判断该用户是否已预约过当天的这个套餐
            int orderExist  = baseMapper.findOrderBySetmealIdAndOrderdateAndMemberId(Integer.parseInt(sid),orderdate,member.getId());
            mid = member.getId();
            if (orderExist  > 0) {
                throw new RuntimeException("已经预约过了!");
            }
        } else {
            member = new Member();
            member.setName((String) map.get("name"));
            member.setSex((String) map.get("sex"));
            member.setIdCard((String) map.get("idCard"));
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date(System.currentTimeMillis()));
            mid = memberService.saveMember(member);
        }
        //4.可以预约
        orderSettingService.updateReservationByOrderDate(orderdate);
        //订单表的录入
        Order order = new Order();
        order.setMemberId(mid);
        order.setOrderDate(DateUtils.parseString2Date(orderdate,"yyyy-MM-dd"));
        order.setSetmealId(Integer.parseInt(sid));
        order.setOrderStatus("未到诊");
        save(order);
        return order;
    }

    @Override
    public Map findOrderInfoByOrderId(int id) {
        return baseMapper.findOrderInfoByOrderId(id);
    }

}
